<template>
  <!-- 搜索 -->
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" :expand="true"  @search="setSearchParams" @reset="setSearchParams">
      <!-- 新增等操作按钮 -->
      <template #actionMore>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['exam:knowledge:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['exam:knowledge:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
      </template>
    </Search>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <Table
      :border="true"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :move="true"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      @row-dblclick="handleRowDblclick" 
    >
      <template #action="{ row }">
        <el-button
            link
            type="primary"
            @click="handleUpdateStatus(row)"
            v-hasPermi="['exam:knowledge:update']"
          >
            {{row.status==1?'启用':'停用'}}
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id)"
            v-hasPermi="['exam:knowledge:update']"
          >
            编辑
          </el-button>
         <el-button
            link
            type="danger"
            @click="handleDelete(row.id)"
            v-hasPermi="['exam:knowledge:delete']"
          >
            删除
          </el-button> 
      </template>
    </Table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <KnowledgeForm ref="formRef" @success="handleOnSuccess" />
</template>
<script lang="ts" setup>
import { allSchemas,getKnowledgeCateTree } from './knowledge.data'
import download from '@/utils/download'
import { KnowledgeApi, KnowledgeVO } from '@/api/exam/knowledge'
import KnowledgeForm from './KnowledgeForm.vue'
/** 知识点 列表 */
defineOptions({ name: 'ExamKnowledge' })
const { tableObject, tableMethods } = useTable({
  getListApi: KnowledgeApi.getKnowledgePage, // 分页接口
  delListApi: KnowledgeApi.deleteKnowledge // 删除接口
})
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  category: undefined,
  title: undefined,
  createTime: []
})
// 搜索的表单
const exportLoading = ref(false) // 导出的加载中
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
const handleRowDblclick=(row)=>{
  openForm('view',row.id)
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await KnowledgeApi.deleteKnowledge(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const handleUpdateStatus = async (row:Object) => {
  try {
    // 删除的二次确认
    await message.confirm("确定要"+(row.status==1?'启用':'停用')+"吗?","提示")
    // 发起删除
    await KnowledgeApi.updateKnowledgeStatus({id:row.id,status:row.status==1?0:1})
    message.success(t('common.updateSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const handleOnSuccess=(data)=>{
  getList()
  console.log(data)

}
/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await KnowledgeApi.exportKnowledge(queryParams)
    download.excel(data, '知识点.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const { push } = useRouter()
/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>